Default constructor

In computer programming languages the term “default constructor” refers to a constructor that is automatically generated in the absence of explicit constructors (and perhaps under other circumstances); this automatically provided constructor is usually a nullary constructor. In specification or discussion of some languages, “default constructor” may additionally refer to any constructor that may be called without arguments, either because it is a nullary constructor or because all of its parameters have default values.

C++

In C++, the standard describes the default constructor for a class as a constructor that can be called with no arguments (this includes a constructor whose parameters all have default arguments).[1] For example:

class MyClass
{
    int x;
    int y;
public:
    MyClass();                                 // constructor declared
};
 
MyClass :: MyClass()                           // constructor defined   
{
    x = 100;
    y = 200;
}
 
int main()
{
    MyClass object_1;                          // object created      
}

In C++, default constructors are significant because they are automatically invoked in certain circumstances:

In the above circumstances, it is an error if the class does not have a default constructor.

The compiler will implicitly define a default constructor if no constructors are explicitly defined for a class. This implicitly-declared default constructor is equivalent to a default constructor defined with a blank body. For example:

class MyClass
{
    .....                                      // No Constructor          
};
int main()
{
    MyClass object_1;                          // No errors      
    ....
}

[2]

If some constructors are defined, but they are all non-default, the compiler will not implicitly define a default constructor. Hence, a default constructor may not exist for a class.This is the reason for a typical error which can be demonstrated by the following example.

class MyClass 
{
private:
    int x;
public:
    MyClass(int y);                            // A Constructor     
};
MyClass :: MyClass(int y)
{
    x = y;
}
int main()
{
    MyClass object_1(100);                     // Constructor Called           
    MyClass object_2;                          // Error: No default Constructor            
    return 0;
}

As a constructor of type other than default is defined the compiler does not define a default constructor and hence the creation of object_2 leads to an error.[3]

Java and C#

In both Java and C#, a "default constructor" refers to a nullary constructor that is automatically generated by the compiler if no constructors have been defined for the class. The default constructor is also empty, meaning that it does nothing. A user defined constructor that takes no parameters is called a default constructor too.[4][5]

References

  1. ^ C++ standard, ISO/IEC 14882:1998, 12.1.5
    C++ standard, ISO/IEC 14882:2003, 12.1.5
  2. ^ Computer Science A Structured Approach Using C++ by Behrouz A. Forouzan and Richard F. Gilberg
  3. ^ Computer Science A Structured Approach Using C++ by Behrouz A. Forouzan and Richard F. Gilberg
  4. ^ Java Language Specification, 3rd edition, section 8.8.9, "Default Constructor".
  5. ^ Using Constructors (C# Programming Guide)